package nextfm.dao;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

import nextfm.model.Administrador;
import nextfm.model.Comum;
import nextfm.model.Usuario;

public class DAOUsuario extends DAOJPA<Usuario> {

	public DAOUsuario() {
		super();
	}

	@SuppressWarnings("unchecked")
	public List<Administrador> findAllAdministradores() {
		try {
			Query q = getManager().createQuery("SELECT a FROM Administrador a");
			return q.getResultList();
		}
		catch (NoResultException e) {
			return null;
		}
		catch (NonUniqueResultException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Comum> findAllUsuariosComuns() {
		try {
			Query q = getManager().createQuery("SELECT c FROM Comum c");
			return q.getResultList();
		}
		catch (NoResultException e) {
			return null;
		}
		catch (NonUniqueResultException e) {
			return null;
		}
	}

	public Usuario findUsuario(String login, String senha) {

		return (Usuario) findByQuery("SELECT u FROM Usuario u WHERE u.login = '" + login + "' AND u.senha = '" + senha + "'");

	}

	public Comum findComum(String login, String senha) {

		Comum comum = (Comum) findUsuario(login, senha);

		if (comum.getEstado() == 0)
			return comum;

		return null;

	}

	public Administrador findAdministrador(String login, String senha, String pin) {

		return (Administrador) findByQuery("SELECT a FROM Administrador a WHERE a.login = '" + login + "' AND a.senha = '" + senha + "' AND a.pin = '" + pin + "' ");

	}

}